package utils;


import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class UStrings {
	
	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" +
		"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private static final String NAME_PATTERN = "[_A-Za-z0-9-]{6,}?";
	private static final String TOKEN_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	 
	
	
	public static boolean isValidEmail(String email) {
		boolean r = false;
		
		try {
			r = Pattern.matches(EMAIL_PATTERN, email);
		}
		catch(PatternSyntaxException e) {
			e.printStackTrace();
			System.err.println("Not a valid email : " + email);
		}
		
		return r;
	}
	
	
	
	public static boolean isValidName(String name) {
		boolean r = false;
		
		try {
			r = Pattern.matches(NAME_PATTERN, name);
		}
		catch(PatternSyntaxException e) {
			e.printStackTrace();
			System.err.println("Not a valid name : " + name);
		}
		
		return r;
	}
	
	
	public static String generateToken(int length) {
		StringBuilder pass = new StringBuilder(length);
		for (int x=0; x<length; x++) {
			int i = (int) (Math.random() * TOKEN_CHARS.length());
			pass.append(TOKEN_CHARS.charAt(i));
		}
		return pass.toString();
	}
	
	public static String generateToken() {
		return generateToken(64);
	}

}
